package guda.stark.web.action.sys.role;

import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.stark.biz.RoleBiz;
import guda.stark.biz.beans.vo.JsonResult;
import guda.stark.biz.form.RoleEditForm;
import guda.stark.biz.form.RoleForm;
import guda.stark.biz.query.RoleQuery;
import guda.stark.biz.util.ErrorCode;
import guda.stark.dal.dataobject.RoleDO;
import guda.stark.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by well on 15/11/8.
 */
@Controller
public class RoleAction {

    @Autowired
    private RoleBiz roleBiz;

    @RequestMapping(value = "sys/role/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        RoleQuery roleQuery = new RoleQuery();
        roleQuery.setPageNo(pageId);
        roleQuery.setPageSize(pageSize);
        modelMap.put("query",roleQuery);
        BizResult bizResult = roleBiz.list(roleQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/role/list.vm";
        } else {
            return "error.vm";
        }
    }

    @RequestMapping(value = "sys/role/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, RoleEditForm roleEditForm,
                       BindingResult result, Map<String,Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = roleBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            roleEditForm.initForm(((RoleDO)bizResult.data.get("roleDO")));
            return "sys/role/edit.vm";
        } else {
            return "error.vm";
        }

    }

    @RequestMapping(value = "sys/role/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = roleBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/resource/detail.vm";
        } else {
            return "error.vm";
        }

    }

    @RequestMapping(value = "sys/role/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, RoleForm roleForm,
                         BindingResult result) {
        return "sys/role/create.vm";
    }

    @RequestMapping(value = "sys/role/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap,@Valid RoleForm roleForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            return "sys/role/create.vm";
        }
        RoleDO roleDO = roleForm.toDO();
        BizResult bizResult = roleBiz.create(roleDO);
        if (bizResult.success) {
            return "redirect:/sys/role/list.htm";
        } else {
            return "error.vm";
        }

    }

    @RequestMapping(value = "sys/role/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,@Valid RoleEditForm roleEditForm,
                           BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            return "sys/role/edit.vm";
        }
        RoleDO roleDO = roleEditForm.toDO();
        BizResult bizResult = roleBiz.update(roleDO);
        if (bizResult.success) {
            return "redirect:/sys/role/list.htm";
        } else {
            return "error.vm";
        }

    }

    @RequestMapping(value = "sys/role/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = roleBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }
}
